import { bg_image } from "./indoorConfig.js";
import {orderUnionPay,listUnpaidOrder} from "../api/parkingRecords/index.js"
export default {
	data() {
		return {
			showOverlay:false,//弹窗
			bg_image,
			payInfo: {},
			list: [],
			renderList: [],
			currentPage: 1,
			query: {
				portId: null,
				mchId: null,
				plateNumber: null
			},
			checkedList: [],
			departmentList: [1, 2]
		}
	},
	onLoad(options) {
		this.query.portId = options.portId.split(',')[0]
		this.query.type = options.portId.split(',')[1]
		this.query.mchId = options.mchId
		this.query.plateNumber = options.plateNumber
		this.getList()
	},
	onReachBottom() {
		this.currentPage += 1
		this._getRenderList()
	},
	onPullDownRefresh(){//页面下拉刷新
		this.fetchSomething()
		uni.stopPullDownRefresh();
	},
	computed: {
		totalPrice() {
			let total = 0;
			this.list.forEach((item) => {
				if (this.checkedList.includes(item.id)) {
					total += Number(item.actualAmount)
				}
			})
			return total.toFixed(2);
		}
	},
	methods: {
		async fetchSomething(query){
			this.getList()
		},
		isIndoor(id) {
			let result = false;
			for (let i = 0; i < this.list.length; i++) {
				if (this.list[i].id === id) {
					result = this.list[i].indoor;
					break;
				}
			}
			return result
		},
		async patchPay() {
			let indoorOrderId = []
			let outdoorOrderIds = [];
			this.checkedList.forEach(_id => {
				if (this.isIndoor(_id)) {
					indoorOrderId.push(_id)
				} else {
					outdoorOrderIds.push(_id)
				}
			})
			let params = {}
			if (this.query.type === '1') {
				params = {
					scene: 2,
					scene2: {
						portId: this.query.portId,
						outdoorOrderIds: outdoorOrderIds
					}
				}
			} else {
				params = {
					scene: 3,
					scene3: {
						portId: this.query.portId,
						indoorOrderIds: indoorOrderId,
						outdoorOrderIds: outdoorOrderIds
					}
				}
			}
			let res = await orderUnionPay(params)
			this.payInfo = res.data || {}
			this.onBridgeReady()
		},
		open() {
			this.showOverlay = true
		},
		handleCancel() {
			this.showOverlay = false
		},
		handleConfirm() {
			this.showOverlay = false
		},
		handleSelectDepartment(item) {},
		handleToggleSelectItem(item) {
			return false;
			if (this.checkedList.includes(item.id)) {
				this.checkedList = this.checkedList.filter(id => {
					return id !== item.id
				})
			} else {
				this.checkedList.push(item.id)
			}
		},
		handleToggleSelectAll() {
			return false;
			if (this.checkedList.length === this.list.length) {
				this.checkedList = []
			} else {
				this.checkedList = this.list.map(item => {
					return item.id
				})
			}
		},
		async getList() {
			let res = await listUnpaidOrder(this.query.portId)
			this.list = res.data || []
			this.checkedList = this.list.map(item => item.id)
			this._getRenderList()
		},
		go(url, item) {
			uni.navigateTo({
				url: `${url}?id=${item.id}&indoor=${item.indoor}&departmentId=${item.departmentId}&type=1&outdoorProjectId=${item.indoor ? '' : item.outdoorProjectId}`
			})
		},
		async onBridgeReady() {
			let Provider =  await uni.getProvider({service: 'payment'})
			let payCanshu = {provider: Provider.provider[0] || 'wxpay',}
			payCanshu = {
				...payCanshu,
				orderInfo:this.payInfo.tradeNo,
			}
			let rePayment = await uni.requestPayment({...payCanshu});
			let flags = rePayment.errMsg == 'requestPayment:ok' && rePayment.resultCode != '6001'
			if(flags) {
				uni.navigateBack()
			}else{
				this.$u.toast('支付失败');
			}
		},
		_getRenderList() {
			if ((this.currentPage - 1) * 10 >= this.list.length) return
			this.renderList = this.renderList.concat(this.list.slice((this.currentPage - 1) * 10, this.currentPage * 10))
		},
	},
}